Completed
Push — master ( f17a0f...fe5154 )
by Rafael S.
03:06
created

main.js ➔ getChunkId_   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 2
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The riff-chunks API.
27
 * @see https://github.com/rochars/riff-chunks
28
 */
29
30
/** @module riffChunks */
31
32
import {unpackFrom, unpackString} from 'byte-data';
33
34
/** @private */
35
const uInt32_ = {bits: 32};
36
/** @type {number} */
37
let head_ = 0;
38
39
/**
40
 * Return the chunks in a RIFF/RIFX file.
41
 * @param {!Uint8Array} buffer The file bytes.
42
 * @return {!Object} The RIFF chunks.
43
 */
44
export function riffChunks(buffer) {
45
    head_ = 0;
46
    let chunkId = getChunkId_(buffer, 0);
47
    uInt32_.be = chunkId == 'RIFX';
48
    let format = unpackString(buffer, 8, 4);
49
    head_ += 4;
50
    return {
51
        chunkId: chunkId,
52
        chunkSize: getChunkSize_(buffer, 0),
53
        format: format,
54
        subChunks: getSubChunksIndex_(buffer)
55
    };
56
}
57
58
/**
59
  * Find a chunk by its fourCC_ in a array of RIFF chunks.
60
  * @param {!Object} chunks The wav file chunks.
61
  * @param {string} chunkId The chunk fourCC_.
62
  * @param {boolean} multiple True if there may be multiple chunks
63
  *    with the same chunkId.
64
  * @return {?Array<!Object>}
65
  */
66
export function findChunk(chunks, chunkId, multiple=false) {
67
  /** @type {!Array<!Object>} */
68
  let chunk = [];
69
  for (let i=0; i<chunks.length; i++) {
70
    if (chunks[i].chunkId == chunkId) {
71
      if (multiple) {
72
        chunk.push(chunks[i]);
73
      } else {
74
        return chunks[i];
75
      }
76
    }
77
  }
78
  if (chunkId == 'LIST') {
79
    return chunk.length ? chunk : null;
80
  }
81
  return null;
82
}
83
84
/**
85
 * Return the sub chunks of a RIFF file.
86
 * @param {!Uint8Array} buffer the RIFF file bytes.
87
 * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk.
88
 * @private
89
 */
90
function getSubChunksIndex_(buffer) {
91
    let chunks = [];
92
    let i = head_;
93
    while(i <= buffer.length - 8) {
94
        chunks.push(getSubChunkIndex_(buffer, i));
95
        i += 8 + chunks[chunks.length - 1].chunkSize;
96
        i = i % 2 ? i + 1 : i;
97
    }
98
    return chunks;
99
}
100
101
/**
102
 * Return a sub chunk from a RIFF file.
103
 * @param {!Uint8Array} buffer the RIFF file bytes.
104
 * @param {number} index The start index of the chunk.
105
 * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk.
106
 * @private
107
 */
108
function getSubChunkIndex_(buffer, index) {
109
    let chunk = {
110
        chunkId: getChunkId_(buffer, index),
111
        chunkSize: getChunkSize_(buffer, index),
112
    };
113
    if (chunk.chunkId == 'LIST') {
114
        chunk.format = unpackString(buffer, index + 8, 4);
115
        head_ += 4;
116
        chunk.subChunks = getSubChunksIndex_(buffer);
117
    } else {
118
        let realChunkSize = chunk.chunkSize % 2 ?
119
            chunk.chunkSize + 1 : chunk.chunkSize;
120
        head_ = index + 8 + realChunkSize;
121
        chunk.chunkData = {
122
            start: index + 8,
123
            end: head_
124
        };
125
    }
126
    return chunk;
127
}
128
129
/**
130
 * Return the fourCC_ of a chunk.
131
 * @param {!Uint8Array} buffer the RIFF file bytes.
132
 * @param {number} index The start index of the chunk.
133
 * @return {string} The id of the chunk.
134
 * @private
135
 */
136
function getChunkId_(buffer, index) {
137
    head_ += 4;
138
    return unpackString(buffer, index, 4);
139
}
140
141
/**
142
 * Return the size of a chunk.
143
 * @param {!Uint8Array} buffer the RIFF file bytes.
144
 * @param {number} index The start index of the chunk.
145
 * @return {number} The size of the chunk without the id and size fields.
146
 * @private
147
 */
148
function getChunkSize_(buffer, index) {
149
    head_ += 4;
150
    return unpackFrom(buffer, uInt32_, index + 4);
151
}
152